package org.jsets.rpc.server;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicLong;

import io.netty.channel.Channel;

public class RequestHander extends AbstractNettyHandler{
	
	private Map<Long, ResponseFuture> futures = new ConcurrentHashMap<>();
	
	private AtomicLong atomicLong = new AtomicLong(1);
	
	public Response syncSendRequest(Channel channel, Request request){
		request.setId(atomicLong.incrementAndGet());
		ResponseFuture future = new ResponseFuture(request);
		futures.put(request.getId(), future);
		channel.write(request);
		
		Response pesponse = null;
		try {
			pesponse = future.get();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		
		return pesponse;		
	}

}
